package com.example.ywj.toutiao;

import android.content.Intent;
import android.graphics.Typeface;
import com.google.android.material.tabs.TabLayout;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.ywj.toutiao.bean.Title;
import com.example.ywj.toutiao.utils.Constant;
import com.example.ywj.toutiao.utils.MyFragmentPageAdapte3;
import com.example.ywj.toutiao.utils.ResponseApi;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class SearchResultActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "SearchResultActivity";
    @BindView(R.id.search_back_result)
    public TextView search_back_result;
    @BindView(R.id.search_fdj_result)
    public TextView search_fdj_result;
    @BindView(R.id.search_editView_result)
    public EditText search_editView_result;
    @BindView(R.id.search_result_tabLayout)
    public TabLayout tabLayout;
    @BindView(R.id.viewpager_search_result)
    public ViewPager viewPager;
    @BindView(R.id.search_delete_result)
    public TextView search_delete_result;
    private String baseUrl = Constant.BASEURL;
    public List<String> strTitle = new ArrayList<>();
    public MyFragmentPageAdapte3 myFragmentPageAdapter3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        String keyword = intent.getStringExtra("keyword");
        initView(keyword);
        getData();
    }
    public void initView(String keyword){
        Typeface font = Typeface.createFromAsset(getAssets(), "iconfont.ttf");
        search_back_result.setTypeface(font);
        search_back_result.setText(getResources().getString(R.string.back));
        search_back_result.setOnClickListener(this);
        search_fdj_result.setTypeface(font);
        search_fdj_result.setText(getResources().getString(R.string.font5));
        search_editView_result.setText(keyword);
        search_delete_result.setTypeface(font);
        search_delete_result.setText(getResources().getString(R.string.chacha));
        search_editView_result.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.i(TAG, "onTextChanged: "+String.valueOf(s));
                if(String.valueOf(s).equals("")){
                    finish();
                }else{
                    search_delete_result.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        search_delete_result.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                search_editView_result.setText("");
            }
        });

    }

    @Override
    protected void onPause() {
        overridePendingTransition(0,0);
        super.onPause();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.search_back_result:
                finish();
                break;
        }
    }
    //获取标签数据
    private void getData(){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        Call<ResponseBody> call = responseApi.getData();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try {
                    json = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Log.i(TAG, "onResponse: "+json);
                JsonObject obj = (JsonObject) new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                if(code == 200 && obj.has("data")){
                    JsonArray data = obj.getAsJsonArray("data");
                    Gson gson = new Gson();
                    List<Title> list = gson.fromJson(data,new TypeToken<ArrayList<Title>>(){}.getType());
                    for(int i = 0;i<list.size();i++){
                        strTitle.add(list.get(i).getTitle());
                    }
                    addTabToTabLayout(strTitle);
                    tabLayout.setupWithViewPager(viewPager);
                    myFragmentPageAdapter3 = new MyFragmentPageAdapte3(getSupportFragmentManager(),strTitle);
                    viewPager.setAdapter(myFragmentPageAdapter3);
                    viewPager.setOffscreenPageLimit(strTitle.size());
                }else {
                    Log.i(TAG, "onResponse: 出错了");
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onFailure: 请求失败");
            }
        });
    }
    private void addTabToTabLayout(List<String> list) {
        for (int i = 0; i < list.size(); i++) {
            tabLayout.addTab(tabLayout.newTab().setText(list.get(i)));
        }
    }
}
